#! /bin/sh

# if any of it looks familiar, I stole it from MPlayer.

for parm in "$@" ; do
  if test "$parm" = "--help" || test "$parm" = "-help" || test "$parm" = "-h" ; then
    cat << EOF

Usage: $0 [OPTIONS]...

Configuration:
  -h, --help             display this help and exit

Compile System:
  --no-cygwin            compile on cygwin with mingw runtime
  --enable-debug         compile with debugging symbols
  --with-socklib=LIB     specify the socket library to link against (if any)

Installation directories:
  --prefix=DIR           use this prefix for installation [/usr/local]
  --bindir=DIR           use this prefix for installing binaries [PREFIX/bin]
  --incdir=DIR           use this prefix for installing headers [PREFIX/include]
  --docdir=DIR           use this prefix for installing docs [PREFIX/share/doc]
  --libdir=DIR           use this prefix for object code libraries [PREFIX/lib]

EOF
    exit 0
  fi
done # for parm in ...

_prefix="/usr/local"
_no_cygwin=no
_debug=no

for ac_option do
  case "$ac_option" in
  --enable-debug)
    _debug=yes
    ;;
  --no-cygwin)
    _no_cygwin=yes
    ;;
  --prefix=*)
    _prefix=`echo $ac_option | cut -d '=' -f 2`
    ;;
  --bindir=*)
    _bindir=`echo $ac_option | cut -d '=' -f 2`
    ;;
  --incdir=*)
    _incdir=`echo $ac_option | cut -d '=' -f 2`
    ;;
  --docdir=*)
    _docdir=`echo $ac_option | cut -d '=' -f 2`
    ;;
  --libdir=*)
    _libdir=`echo $ac_option | cut -d '=' -f 2`
    ;;
  --with-socklib=*)
    _socklib=`echo $ac_option | cut -d '=' -f 2`
    ;;

  *)
    echo "Unknown parameter: $ac_option"
    exit 1
    ;;

  esac
done

test -z "$_bindir" && _bindir="$_prefix/bin"
test -z "$_incdir" && _incdir="$_prefix/include"
test -z "$_docdir" && _docdir="$_prefix/share/doc"
test -z "$_libdir" && _libdir="$_prefix/lib"

if test -z "$CFLAGS" ; then
  CFLAGS="-O2"
fi
if test "$_debug" = "yes"; then
  CFLAGS="$CFLAGS -g -Wall"
fi

_cygwin=`uname -s | grep -i cygwin`;
_mingw=`uname -s | grep -i mingw`;
if test "$_cygwin" != "" && test "$_no_cygwin" = "yes"; then
  _mingw=yes
  CFLAGS="$CFLAGS -mno-cygwin"
  LDFLAGS="$LDFLAGS -mno-cygwin -enable-stdcall-fixup"
fi
if test "$_mingw" != "" && test -z "$_socklib"; then
  _socklib="-lws2_32"
fi

#############################################################################
echo "Creating config.mak"
cat > config.mak << EOF
# -------- Generated by configure -----------

BINDIR = $_bindir
LIBDIR = $_libdir
INCDIR = $_incdir
DOCDIR = $_docdir

D_BINDIR = \$(DESTDIR)\$(BINDIR)
D_LIBDIR = \$(DESTDIR)\$(LIBDIR)
D_INCDIR = \$(DESTDIR)\$(INCDIR)
D_DOCDIR = \$(DESTDIR)\$(DOCDIR)

CFLAGS = $CFLAGS
LDFLAGS = $LDFLAGS

CYGWIN = $_cygwin
MINGW = $_mingw
SOCKLIB = $_socklib

EOF

echo "Config files successfully generated by ./configure !"

